# coding=utf-8

import web
import utils

__all__ = ('app_account', 'UserSession',)

urls = (
	'(?i)/login', 'applications.account.views.login',
	'(?i)/register', 'applications.account.views.register',
	'', 'applications.account.views.index',
)

app_account = web.application(urls, locals())

# TODO: 表示一个用户会话对象
class UserSession(object):
	def __init__(self):
		self.uid = 0
		self._resolve_cookie()
	
	# TODO: 获取cookie中的信息
	def _resolve_cookie(self):
		val = utils.decrypt(web.cookies().get('su', None))
		if val:
			vals = val.split('|')
			if len(vals) == 2:
				self.uid = long(vals[0])
	
	# TODO: 表示当前会话用户是否登录
	def _is_logined(self):
		return self.uid > 0
	
	# TODO: 表示当前会话用户是否登录
	logined = property(_is_logined)
	
	# TODO: 退出当前会话用户的登录状态
	def logout(self, redir = True):
		# 清空cookie
		web.setcookie('su', None)
		if redir:
			# 转向到登录页
			web.seeother('/account/login')
	
	# TODO: 获取当前会话的用户信息（获取user_info表记录）
	def resolve_info(self):
		return None
	
	# TODO: 获取当前会话用户（静态方法）
	@staticmethod
	def current():
		return UserSession()
